package com.haha.extend.textview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.TextView;

import com.Long.library.R;
import com.haha.extend.textview.StateAddFilterTextView;

import java.lang.reflect.Field;


/**
 * Created by Administrator on 2016/6/20 0020.
 */
public class StrokeStateTextView extends StateAddFilterTextView {
    private int strokeWidth = 5;
    private int strokeColor = 0x000000;
    private int textColor = 0xFFFFFF;
    public StrokeStateTextView(Context context) {
        this(context,null);
    }

    public StrokeStateTextView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public StrokeStateTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.StrokeStateTextView);
        strokeWidth = ta.getDimensionPixelSize(R.styleable.StrokeStateTextView_strokeWidth,strokeWidth);
        strokeColor = ta.getColor(R.styleable.StrokeStateTextView_strokeColor,strokeColor);
        ta.recycle();
        textColor = getCurrentTextColor();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.setTextColor(strokeColor);
        setTextColorUseReflection(strokeColor);
        getPaint().setColor(strokeColor);
        getPaint().setStrokeWidth(strokeWidth);
        getPaint().setStyle(Paint.Style.STROKE);
        super.onDraw(canvas);

        setTextColorUseReflection(textColor);
        getPaint().setStrokeWidth(0);
        getPaint().setStyle(Paint.Style.FILL);
        super.onDraw(canvas);
    }

    private void setTextColorUseReflection(int color){
        Field textColorField;
        try {
            textColorField = TextView.class.getDeclaredField("mCurTextColor");
            textColorField.setAccessible(true);
            textColorField.set(this,color);
            textColorField.setAccessible(false);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        getPaint().setColor(color);
    }
}
